home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / GridLayout.java < prev    next >
Text File  |  1998-09-22  |  13KB  |  375 lines

  1. /*
  2.  * @(#)GridLayout.java    1.18 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt;
  16.  
  17. /**
  18.  * The <code>GridLayout</code> class is a layout manager that 
  19.  * lays out a container's components in a rectangular grid. 
  20.  * <p>
  21.  * The container is divided into equal-sized rectangles, 
  22.  * and one component is placed in each rectangle. 
  23.  * <p>
  24.  * For example, the following is an applet that lays out six buttons 
  25.  * into three rows and two columns: 
  26.  * <p>
  27.  * <hr><blockquote><pre>
  28.  * import java.awt.*;
  29.  * import java.applet.Applet;
  30.  * public class ButtonGrid extends Applet {
  31.  *     public void init() {
  32.  *         setLayout(new GridLayout(3,2));
  33.  *         add(new Button("1"));
  34.  *         add(new Button("2"));
  35.  *         add(new Button("3"));
  36.  *         add(new Button("4"));
  37.  *         add(new Button("5"));
  38.  *         add(new Button("6"));
  39.  *     }
  40.  * }
  41.  * </pre></blockquote><hr>     
  42.  * <p>
  43.  * It produces the following output:
  44.  * <p>
  45.  * <img src="images-awt/GridLayout-1.gif" 
  46.  * ALIGN=center HSPACE=10 VSPACE=7>
  47.  *
  48.  * @version 1.18, 07/01/98
  49.  * @author 
  50.  * @since   JDK1.0
  51.  */
  52. public class GridLayout implements LayoutManager, java.io.Serializable {
  53.     int hgap;
  54.     int vgap;
  55.     int rows;
  56.     int cols;
  57.  
  58.     /**
  59.      * Creates a grid layout with a default of one column per component,
  60.      * in a single row.
  61.      * @since JDK1.1
  62.      */
  63.     public GridLayout() {
  64.     this(1, 0, 0, 0);
  65.     }
  66.  
  67.     /**
  68.      * Creates a grid layout with the specified number of rows and 
  69.      * columns. All components in the layout are given equal size. 
  70.      * <p>
  71.      * One, but not both, of <code>rows</code> and <code>cols</code> can 
  72.      * be zero, which means that any number of objects can be placed in a 
  73.      * row or in a column. 
  74.      * @param     rows   the rows, with the value zero meaning 
  75.      *                   any number of rows.
  76.      * @param     cols   the columns, with the value zero meaning 
  77.      *                   any number of columns.
  78.      * @since     JDK1.0
  79.      */
  80.     public GridLayout(int rows, int cols) {
  81.     this(rows, cols, 0, 0);
  82.     }
  83.  
  84.     /**
  85.      * Creates a grid layout with the specified number of rows and 
  86.      * columns. All components in the layout are given equal size. 
  87.      * <p>
  88.      * In addition, the horizontal and vertical gaps are set to the 
  89.      * specified values. Horizontal gaps are placed at the left and 
  90.      * right edges, and between each of the columns. Vertical gaps are 
  91.      * placed at the top and bottom edges, and between each of the rows. 
  92.      * <p>
  93.      * One, but not both, of <code>rows</code> and <code>cols</code> can 
  94.      * be zero, which means that any number of objects can be placed in a 
  95.      * row or in a column. 
  96.      * @param     rows   the rows, with the value zero meaning 
  97.      *                   any number of rows.
  98.      * @param     cols   the columns, with the value zero meaning 
  99.      *                   any number of columns.
  100.      * @param     hgap   the horizontal gap. 
  101.      * @param     vgap   the vertical gap. 
  102.      * @exception   IllegalArgumentException  if the of <code>rows</code> 
  103.      *                   or <code>cols</code> is invalid.
  104.      * @since     JDK1.0
  105.      */
  106.     public GridLayout(int rows, int cols, int hgap, int vgap) {
  107.     if ((rows == 0) && (cols == 0)) {
  108.         throw new IllegalArgumentException("rows and cols cannot both be zero");
  109.     }
  110.     this.rows = rows;
  111.     this.cols = cols;
  112.     this.hgap = hgap;
  113.     this.vgap = vgap;
  114.     }
  115.  
  116.     /**
  117.      * Gets the number of rows in this layout.
  118.      * @return    the number of rows in this layout.
  119.      * @since     JDK1.1
  120.      */
  121.     public int getRows() {
  122.     return rows;
  123.     }
  124.  
  125.     /**
  126.      * Sets the number of rows in this layout to the specified value.
  127.      * @param        rows   the number of rows in this layout.
  128.      * @exception    IllegalArgumentException  if the value of both 
  129.      *               <code>rows</code> and <code>cols</code> is set to zero.
  130.      * @since        JDK1.1
  131.      */
  132.     public void setRows(int rows) {
  133.     if ((rows == 0) && (this.cols == 0)) {
  134.         throw new IllegalArgumentException("rows and cols cannot both be zero");
  135.     }
  136.     this.rows = rows;
  137.     }
  138.  
  139.     /**
  140.      * Gets the number of columns in this layout.
  141.      * @return     the number of columns in this layout.
  142.      * @since      JDK1.1
  143.      */
  144.     public int getColumns() {
  145.     return cols;
  146.     }
  147.  
  148.     /**
  149.      * Sets the number of columns in this layout to the specified value.
  150.      * @param        cols   the number of columns in this layout.
  151.      * @exception    IllegalArgumentException  if the value of both 
  152.      *               <code>rows</code> and <code>cols</code> is set to zero.
  153.      * @since        JDK1.1
  154.      */
  155.     public void setColumns(int cols) {
  156.     if ((cols == 0) && (this.rows == 0)) {
  157.         throw new IllegalArgumentException("rows and cols cannot both be zero");
  158.     }
  159.     this.cols = cols;
  160.     }
  161.  
  162.     /**
  163.      * Gets the horizontal gap between components.
  164.      * @return       the horizontal gap between components.
  165.      * @since        JDK1.1
  166.      */
  167.     public int getHgap() {
  168.     return hgap;
  169.     }
  170.     
  171.     /**
  172.      * Sets the horizontal gap between components to the specified value.
  173.      * @param        hgap   the horizontal gap between components.
  174.      * @since        JDK1.1
  175.      */
  176.     public void setHgap(int hgap) {
  177.     this.hgap = hgap;
  178.     }
  179.     
  180.     /**
  181.      * Gets the vertical gap between components.
  182.      * @return       the vertical gap between components.
  183.      * @since        JDK1.1
  184.      */
  185.     public int getVgap() {
  186.     return vgap;
  187.     }
  188.     
  189.     /**
  190.      * Sets the vertical gap between components to the specified value.
  191.      * @param         vgap  the vertical gap between components.
  192.      * @since        JDK1.1
  193.      */
  194.     public void setVgap(int vgap) {
  195.     this.vgap = vgap;
  196.     }
  197.  
  198.     /**
  199.      * Adds the specified component with the specified name to the layout.
  200.      * @param name the name of the component.
  201.      * @param comp the component to be added.
  202.      * @since JDK1.0
  203.      */
  204.     public void addLayoutComponent(String name, Component comp) {
  205.     }
  206.  
  207.     /**
  208.      * Removes the specified component from the layout. 
  209.      * @param comp the component to be removed.
  210.      * @since JDK1.0
  211.      */
  212.     public void removeLayoutComponent(Component comp) {
  213.     }
  214.  
  215.     /** 
  216.      * Determines the preferred size of the container argument using 
  217.      * this grid layout. 
  218.      * <p>
  219.      * The preferred width of a grid layout is the largest preferred 
  220.      * width of any of the widths in the container times the number of 
  221.      * columns, plus the horizontal padding times the number of columns 
  222.      * plus one, plus the left and right insets of the target container. 
  223.      * <p>
  224.      * The preferred height of a grid layout is the largest preferred 
  225.      * height of any of the widths in the container times the number of 
  226.      * rows, plus the vertical padding times the number of rows plus one, 
  227.      * plus the top and left insets of the target container. 
  228.      * 
  229.      * @param     target   the container in which to do the layout.
  230.      * @return    the preferred dimensions to lay out the 
  231.      *                      subcomponents of the specified container.
  232.      * @see       java.awt.GridLayout#minimumLayoutSize 
  233.      * @see       java.awt.Container#getPreferredSize()
  234.      * @since     JDK1.0
  235.      */
  236.     public Dimension preferredLayoutSize(Container parent) {
  237.       synchronized (parent.getTreeLock()){
  238.     Insets insets = parent.getInsets();
  239.     int ncomponents = parent.getComponentCount();
  240.     int nrows = rows;
  241.     int ncols = cols;
  242.  
  243.     if (nrows > 0) {
  244.         ncols = (ncomponents + nrows - 1) / nrows;
  245.     } else {
  246.         nrows = (ncomponents + ncols - 1) / ncols;
  247.     }
  248.     int w = 0;
  249.     int h = 0;
  250.     for (int i = 0 ; i < ncomponents ; i++) {
  251.         Component comp = parent.getComponent(i);
  252.         Dimension d = comp.getPreferredSize();
  253.         if (w < d.width) {
  254.         w = d.width;
  255.         }
  256.         if (h < d.height) {
  257.         h = d.height;
  258.         }
  259.     }
  260.     return new Dimension(insets.left + insets.right + ncols*w + (ncols-1)*hgap, 
  261.                  insets.top + insets.bottom + nrows*h + (nrows-1)*vgap);
  262.       }
  263.     }
  264.  
  265.     /**
  266.      * Determines the minimum size of the container argument using this 
  267.      * grid layout. 
  268.      * <p>
  269.      * The minimum width of a grid layout is the largest minimum width 
  270.      * of any of the widths in the container times the number of columns, 
  271.      * plus the horizontal padding times the number of columns plus one, 
  272.      * plus the left and right insets of the target container. 
  273.      * <p>
  274.      * The minimum height of a grid layout is the largest minimum height 
  275.      * of any of the widths in the container times the number of rows, 
  276.      * plus the vertical padding times the number of rows plus one, plus 
  277.      * the top and left insets of the target container. 
  278.      *  
  279.      * @param       target   the container in which to do the layout.
  280.      * @return      the minimum dimensions needed to lay out the 
  281.      *                      subcomponents of the specified container.
  282.      * @see         java.awt.GridLayout#preferredLayoutSize
  283.      * @see         java.awt.Container#doLayout
  284.      * @since       JDK1.0
  285.      */
  286.     public Dimension minimumLayoutSize(Container parent) {
  287.       synchronized (parent.getTreeLock()){
  288.     Insets insets = parent.getInsets();
  289.     int ncomponents = parent.getComponentCount();
  290.     int nrows = rows;
  291.     int ncols = cols;
  292.  
  293.     if (nrows > 0) {
  294.         ncols = (ncomponents + nrows - 1) / nrows;
  295.     } else {
  296.         nrows = (ncomponents + ncols - 1) / ncols;
  297.     }
  298.     int w = 0;
  299.     int h = 0;
  300.     for (int i = 0 ; i < ncomponents ; i++) {
  301.         Component comp = parent.getComponent(i);
  302.         Dimension d = comp.getMinimumSize();
  303.         if (w < d.width) {
  304.         w = d.width;
  305.         }
  306.         if (h < d.height) {
  307.         h = d.height;
  308.         }
  309.     }
  310.     return new Dimension(insets.left + insets.right + ncols*w + (ncols-1)*hgap, 
  311.                  insets.top + insets.bottom + nrows*h + (nrows-1)*vgap);
  312.       }
  313.     }
  314.  
  315.     /** 
  316.      * Lays out the specified container using this layout. 
  317.      * <p>
  318.      * This method reshapes the components in the specified target 
  319.      * container in order to satisfy the constraints of the 
  320.      * <code>GridLayout</code> object. 
  321.      * <p>
  322.      * The grid layout manager determines the size of individual 
  323.      * components by dividing the free space in the container into 
  324.      * equal-sized portions according to the number of rows and columns 
  325.      * in the layout. The container's free space equals the container's 
  326.      * size minus any insets and any specified horizontal or vertical 
  327.      * gap. All components in a grid layout are given the same size. 
  328.      *  
  329.      * @param      target   the container in which to do the layout.
  330.      * @see        java.awt.Container
  331.      * @see        java.awt.Container#doLayout
  332.      * @since      JDK1.0
  333.      */
  334.     public void layoutContainer(Container parent) {
  335.       synchronized (parent.getTreeLock()) {
  336.     Insets insets = parent.getInsets();
  337.     int ncomponents = parent.getComponentCount();
  338.     int nrows = rows;
  339.     int ncols = cols;
  340.  
  341.     if (ncomponents == 0) {
  342.         return;
  343.     }
  344.     if (nrows > 0) {
  345.         ncols = (ncomponents + nrows - 1) / nrows;
  346.     } else {
  347.         nrows = (ncomponents + ncols - 1) / ncols;
  348.     }
  349.     int w = parent.width - (insets.left + insets.right);
  350.     int h = parent.height - (insets.top + insets.bottom);
  351.     w = (w - (ncols - 1) * hgap) / ncols;
  352.     h = (h - (nrows - 1) * vgap) / nrows;
  353.  
  354.     for (int c = 0, x = insets.left ; c < ncols ; c++, x += w + hgap) {
  355.         for (int r = 0, y = insets.top ; r < nrows ; r++, y += h + vgap) {
  356.         int i = r * ncols + c;
  357.         if (i < ncomponents) {
  358.             parent.getComponent(i).setBounds(x, y, w, h);
  359.         }
  360.         }
  361.     }
  362.       }
  363.     }
  364.     
  365.     /**
  366.      * Returns the string representation of this grid layout's values.
  367.      * @return     a string representation of this grid layout.
  368.      * @since      JDK1.0
  369.      */
  370.     public String toString() {
  371.     return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + 
  372.                            ",rows=" + rows + ",cols=" + cols + "]";
  373.     }
  374. }
  375.